package com.wei.controller;


import com.wei.entity.GdGasfee;
import com.wei.entity.GdWaterfee;
import com.wei.mapper.GdGasfeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李泽伟
 * @since 2022-03-08
 */
@RestController
@RequestMapping
public class GdGasfeeController {

    @Autowired
    private GdGasfeeMapper gasfeeMapper;

    @RequestMapping("/getGasfees")
    public List<GdGasfee> getGasfee(){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        return gasfeeMapper.selectByMap(map);
    }

    @RequestMapping("/addGasfee")
    public void addGasfee(@RequestParam Map<String,Object> gasfeeObj){
        GdGasfee gasfee =setParam(gasfeeObj);
        gasfeeMapper.insert(gasfee);
    }

    @RequestMapping("/updateGasfee")
    public void updateGasfee(@RequestParam Map<String,Object> gasfeeObj){
        GdGasfee gasfee = setParam(gasfeeObj);
        gasfeeMapper.updateById(gasfee);
    }

    @RequestMapping("/deleteGasfee")
    public void deleteGasfee(@RequestParam String gasfeeId){
        gasfeeMapper.deleteById(gasfeeId);
    }

    /**
     * 通过房产地址、月份获取气费记录，用于月度账单的保存生成(管理员)
     * @param paramObj
     * @return
     */
    @RequestMapping("/getGasfee")
    public Double hasGasfee(@RequestParam Map<String,Object> paramObj){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        map.put("gasfee_date", paramObj.get("feeDate"));
        map.put("property_address", paramObj.get("propertyAddress"));
        return gasfeeMapper.selectByMap(map).get(0).getGasfeeTotalfee();
    }

    /**
     * 通过房产地址获取气费记录，用于气费的查询(普通用户)
     * @param address
     * @return
     */
    @RequestMapping("/getGasfeeByAddress")
    public List<GdGasfee> getGasfeeByAddress(@RequestParam String address){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        map.put("property_address", address);
        return gasfeeMapper.selectByMap(map);
    }

    /**
     * 设置参数方法
     * @param gasfeeObj
     * @return
     */
    private GdGasfee setParam(@RequestParam Map<String, Object> gasfeeObj) {
        GdGasfee gasfee=new GdGasfee();
        gasfee.setGasfeeId((String) gasfeeObj.get("gasfeeId"));
        gasfee.setGasfeeLreading(Integer.parseInt(gasfeeObj.get("gasfeeLreading").toString()));
        gasfee.setGasfeeTreading(Integer.parseInt(gasfeeObj.get("gasfeeTreading").toString()));
        gasfee.setGasfeeAmount(Integer.parseInt(gasfeeObj.get("gasfeeAmount").toString()));
        gasfee.setGasfeePrice(Double.parseDouble(gasfeeObj.get("gasfeePrice").toString()));
        gasfee.setGasfeeTotalfee(Double.parseDouble(gasfeeObj.get("gasfeeTotalfee").toString()));
        gasfee.setGasfeeDate((String) gasfeeObj.get("gasfeeDate"));
        gasfee.setGasfeeForeignkey((String) gasfeeObj.get("gasfeeForeignkey"));
        gasfee.setPropertyAddress((String) gasfeeObj.get("propertyAddress"));
        return gasfee;
    }

}

